﻿using System.Data.Entity;
using System;
using System.Web;

namespace CharacterCreation.Web.Models {
    public class CharacterContext : DbContext {

        public DbSet<Player> Players { get; set; }
        public DbSet<Character> Characters { get; set; }
        public DbSet<Skill> Skills { get; set; }
        public DbSet<Race> Races { get; set; }
        public DbSet<Special> Specials { get; set; }
        public DbSet<Energy> Energies { get; set; }

        public DbSet<CharacterSkill> CharacterSkills { get; set; }
        public DbSet<CharacterSpecial> CharacterSpecials { get; set; }
        public DbSet<CharacterEnergy> CharacterEnergies { get; set; }
        public DbSet<XPDetail> XPDetails { get; set; }
        public DbSet<BonusDetail> BonusDetails { get; set; }

        public DbSet<Strength> Strengths { get; set; }
        public DbSet<Dexterity> Dexterities { get; set; }
        public DbSet<Fighting> Fightings { get; set; }
        public DbSet<Constitution> Constitutions { get; set; }
        public DbSet<Perception> Perceptions { get; set; }
        public DbSet<Intelligence> Intelligences { get; set; }
        public DbSet<WillPower> WillPowers { get; set; }
        public DbSet<Charisma> Charismas { get; set; }

        public DbSet<Equipment> Equipment { get; set; }
        public DbSet<Note> Notes { get; set; }

        public DbSet<SchoolOfMagic> SchoolsOfMagic { get; set; }

        public CharacterContext()
            : base("CharacterDatabase") { }

        public CharacterContext(bool autoGenerateProxies = false)
            : base("CharacterDatabase") {
            if (autoGenerateProxies)
                this.Configuration.ProxyCreationEnabled = false;
        }

        public CharacterContext GetInstance() {
            return new CharacterContext();
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder) {
            base.OnModelCreating(modelBuilder);
            modelBuilder.Entity<SchoolOfMagic>().HasMany(s => s.Components).WithRequired(c => c.SchoolOfMagic).WillCascadeOnDelete();
            modelBuilder.Entity<SchoolOfMagic>().HasMany(s => s.Spells).WithRequired(c => c.SchoolOfMagic).WillCascadeOnDelete();
        }

        public override int SaveChanges() {
            try {
                foreach (var item in this.ChangeTracker.Entries()) {
                    if (typeof(BaseModel).IsAssignableFrom(item.Entity.GetType())) {
                        if (item.State == System.Data.EntityState.Added) {
                            (item.Entity as BaseModel).Id = Guid.NewGuid();
                            (item.Entity as BaseModel).CreatedBy = HttpContext.Current.User.Identity.Name;
                            (item.Entity as BaseModel).DateCreated = DateTime.Now;
                        }
                        if (item.State == System.Data.EntityState.Modified) {
                            (item.Entity as BaseModel).ModifiedBy = HttpContext.Current.User.Identity.Name;
                            (item.Entity as BaseModel).DateModified = DateTime.Now;
                        }
                    }
                }
                var result = base.SaveChanges();
                return result;
            }
            catch {
                return -1;
            }
        }
    }

    public class InitContext : CreateDatabaseIfNotExists<CharacterContext> {
        protected override void Seed(CharacterContext context) {
            base.Seed(context);

            Skill skill1 = new Skill() { DisplayName = "Swimming", Name = "Swimming" };
            context.Skills.Add(skill1);

            Skill skill2 = new Skill() { DisplayName = "Rope Use", Name = "Rope Use" };
            context.Skills.Add(skill2);

            context.SaveChanges();
        }
    }
}